<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="用户姓名" prop="userName"
                :rules="[
                 ]">
              <el-input v-model="inputForm.userName" placeholder="请填写用户姓名"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="用户手机号" prop="userPhone"
                :rules="[
                 ]">
              <el-input v-model="inputForm.userPhone" placeholder="请填写用户手机号"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="用户头像" prop="picture"
                :rules="[
                  {required: true, message:'用户头像不能为空', trigger:'blur'}
                 ]">
              <!-- <el-input v-model="inputForm.picture" placeholder="请填写用户头像"     ></el-input> -->
               <el-upload 
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/dlyrl/hr/weixinuser/weixinuser`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file) => {$window.open ((file.response && file.response.url) || file.url,'_blank') }"
                    :on-success="(response, file, fileList) => {
                      inputForm.picture = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.picture = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="5"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="pictureArra">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">添加相关附件</div>
                  </el-upload>						
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="活动" prop="event.id"
                :rules="[
                 ]">
          <GridSelect
            title="选择活动"
            labelName = 'title'
            labelValue = 'id'
            :value = "inputForm.event.id"
            :limit="1"
            @getValue='(value) => {inputForm.event.id=value}'
            :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'title',
              label: '标题'
            }
            ]"
            :searchs="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'title',
              label: '标题'
            }
            ]"
            dataListUrl="/dlyrl/hr/eventofticket/eventWeixinInformation/list"
            entityBeanName="eventWeixinInformation"
            queryEntityUrl="/dlyrl/hr/eventofticket/eventWeixinInformation/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="邀请人姓名" prop="invitationUserName"
                :rules="[
                 ]">
              <el-input v-model="inputForm.invitationUserName" placeholder="请填写邀请人姓名"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="邀请人电话" prop="invitationPhone"
                :rules="[
                 ]">
              <el-input v-model="inputForm.invitationPhone" placeholder="请填写邀请人电话"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="公司" prop="companyName"
                :rules="[
                 ]">
              <el-input v-model="inputForm.companyName" placeholder="请填写公司"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="部门" prop="department"
                :rules="[
                 ]">
              <el-input v-model="inputForm.department" placeholder="请填写部门"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="岗位" prop="postName"
                :rules="[
                 ]">
              <el-input v-model="inputForm.postName" placeholder="请填写岗位"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import GridSelect from '@/components/gridSelect'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        pictureArra: [],
        inputForm: {
          id: '',
          picture: '',
          userName: '',
          userPhone: '',
          event: {
            id: ''
          },
          invitationUserName: '',
          invitationPhone: '',
          companyName: '',
          department: '',
          postName: ''
        }
      }
    },
    components: {
      GridSelect
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建活动报名用户信息表`
        } else if (method === 'edit') {
          this.title = '修改活动报名用户信息表'
        } else if (method === 'view') {
          this.title = '查看活动报名用户信息表'
        }
        this.pictureArra = []
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/dlyrl/hr/weixinuser/wenxinUser/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.wenxinUser)
              this.inputForm.picture.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.pictureArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/dlyrl/hr/weixinuser/wenxinUser/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      },
			beforeAvatarUpload (file) {
				const isJPG = file.type.indexOf('image/') >= 0
				const isLt2M = file.size / 1024 / 1024 < 5

				if (!isJPG) {
				this.$message.error('上传头像只能是图片格式!')
				return false
				}
				if (!isLt2M) {
				this.$message.error('上传头像图片大小不能超过 2MB!')
				return false
				}
				return true
     		},
    }
  }
</script>
<style scoped>
/* 头像 */
	.headProtait {
		border: 1px solid #d8d8d8;
		border-radius: 5px;
		height: 183px;
		width:100%;
	}
</style>